#include "InvokeInstruction.h"

std::unordered_map<std::string, std::shared_ptr<InvokeInstruction>> InvokeInstruction::invokeInstMap;

void InvokeInstruction::RegisterInstruction(std::shared_ptr<InvokeInstruction> inst)
{
    invokeInstMap.insert(make_pair(inst->GetInstName(), inst));
}

std::string InvokeInstruction::GetHelp()
{
    std::string usage;
    if (!this->GetArgs().empty()) {
        usage = usage + "\n     " + "usage:     -" + this->GetInstName() + " " + this->GetArgs();
    }
    char buf[1024];
    sprintf(buf, "-%-14s %s%s", instName.c_str(), helpInfo.c_str(), usage.c_str());
    return std::string(buf);
}

std::string InvokeInstruction::GetInvokeOptStr()
{
    std::string optStr;
    for (const auto &item : invokeInstMap) {
        optStr += item.first;
        if (!item.second->GetArgs().empty()) {
            optStr += ":";
        }
    }
    return optStr;
}